import { userLogin, userLogout } from '@/api/pdm/login/loginApi';
import { getToken, setToken, removeToken } from '@/utils/auth';
// import { resetRouter } from '@/router';
import { getLocalStore, setLocalStore } from '@/utils/store';

import { sha256 } from 'js-sha256';

const state = {
  token: getToken(),
  username: '',
  displayName: '',
  avatar: '',
  deptId: '',
  dept: '',
  privilegeCodes: [],
  roles: 'normal'
}

const mutations = {
  SET_TOKEN: (state, token) => {
    // cookies 更新用户 access_token
    setToken(token)
    state.token = token
  },

  SET_NAME: (state, username) => {
    state.username = username
  },
  SET_DISPLAYNAME: (state, displayName) => {
    state.displayName = displayName
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_DEPTID: (state, deptId) => {
    state.deptId = deptId
  },
  SET_DEPT: (state, dept) => {
    state.dept = dept
  },

  SET_PRIVILEGECODES: (state, privilegeCodes) => {
    state.privilegeCodes = privilegeCodes
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  /**
   * 重置用户信息
   */
  RESET: (state) => {
    Object.assign(state, {
      token: '',
      username: '',
      displayName: '',
      avatar: '',
      deptId: '',
      dept: '',
      roles: 'normal',
      privilegeCodes: []
    })
  }
}

const actions = {
  // 用户登陆
  async login({ commit }, userInfo) {
    const { username: uname, password, domainId } = userInfo;
    // 请求登陆接口, Login接口中会判断关联账户情况 update by shengl 20210419
    try {
      const res = await userLogin({ userName: uname.trim(), password: sha256(password), domainId });
      return res;
    } catch(err) {
      console.log(err);
      return false;
    }
    // vuex 更新用户 token
    
  },
  // 用户信息
  async getInfo({ commit, state }) {
    const username = getLocalStore('username');
    const userInfo = {};
    // const userInfo = await getInfo(username);
    // vuex 更新用户
    // commit('SET_NAME', userInfo.userid);
    // commit('SET_DISPLAYNAME', userInfo.username);

    return userInfo;
  },
  // 获取用户角色
  async getRoles({ commit, state }) {
    const userName = state.username;

    // const result = await queryJacUserSecLevelByName(userName);
    if (result.code === 200) {
      if (result.data.AUTH === 'normal') {
        commit('SET_ROLES', 'normal');
      } else if (result.data.AUTH === 'leader') {
        commit('SET_ROLES', 'leader');
      }
    } else {
      commit('SET_ROLES', 'editor');
    }
    return result.data.AUTH;
  },
  // 设置用户头像
  setUserAvatar({
    commit
  }, data) {
    return new Promise(resolve => {
      commit('SET_AVATAR', data);
    })
  },
  // 用户登出
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      userLogout().then(() => {
        sessionStorage.clear();
        localStorage.clear();
        resolve();
      }).catch(error => {
        reject(error);
      })
    })
  },
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '');
      commit('SET_PRIVILEGECODES', []);
      // removeToken();
      resolve();
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

